package com.piece.module.file.util.pdf;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.action.PdfAction;
import com.itextpdf.kernel.pdf.navigation.PdfDestination;
import com.itextpdf.kernel.pdf.navigation.PdfExplicitDestination;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;
import com.itextpdf.layout.properties.OverflowPropertyValue;
import com.itextpdf.layout.properties.Property;
import com.itextpdf.layout.properties.TextAlignment;

/**
 * Pdf通用模型
 **/
public class PdfComponent {

    /**
     * 目录分界线
     */
    public static Paragraph getCatelogDottedLine(int type) {
        Paragraph element;
        if (type == 1) {
            element = new Paragraph("------------------------------------------------------------------------");
        } else if (type == 2) {
            element = new Paragraph("-----------------------------------------------");
        } else if (type == 3) {
            element = new Paragraph("-------------------------------------");
        } else {
            element = new Paragraph("----------------------------");
        }
        element.setProperty(Property.OVERFLOW_X, OverflowPropertyValue.HIDDEN);
        element.setTextAlignment(TextAlignment.RIGHT);
        element.setMarginRight(20);
        return element;
    }

    /**
     * 默认单元格
     */
    public static Cell getDefaultCell() {
        return new Cell().addStyle(PdfStyle.getDefaultCell());
    }

    /**
     * 默认表格
     */
    public static Cell getTableCell() {
        return new Cell().setTextAlignment(TextAlignment.CENTER).addStyle(PdfStyle.getTableCell());
    }

    /**
     * 默认合并单元格
     */
    public static Cell getDefaultCell(int rowspan, int colspan) {
        return new Cell(rowspan, colspan).addStyle(PdfStyle.getDefaultCell());
    }

    /**
     * 目录单元格
     */
    public static Cell getCatelogCell() {
        return new Cell().addStyle(PdfStyle.getCatelogCell());
    }

    /**
     * 目录单元格
     */
    public static Cell getCatelogCell(int colsapn) {
        return new Cell(1, colsapn).addStyle(PdfStyle.getCatelogCell());
    }

    /**
     * 无边框单元格
     */
    public static Cell getSignCell() {
        return new Cell().addStyle(PdfStyle.getSignCell());
    }

    /**
     * 默认文本
     */
    public static Paragraph getSignParagraph(String text) {
        return new Paragraph(text).addStyle(PdfStyle.getSignStyle());
    }

    /**
     * 默认二级标题
     */
    public static Text getSecondTitle(String context) {
        return new Text(context).addStyle(PdfStyle.getSecondTitleStyle());
    }

    /**
     * 默认三级标题
     */
    public static Text getThirdTitle(String context) {
        return new Text(context).addStyle(PdfStyle.getThirdTitleStyle());
    }

    /**
     * 默认页头
     */
    public static Paragraph getHeaderLineText(String context) {
        return new Paragraph(context).setTextAlignment(TextAlignment.CENTER).setFontSize(12).setMarginTop(-20);
    }

    /**
     * 默认标题
     */
    public static Paragraph getTitleParagraph(Text context) {
        return new Paragraph(context).setMargin(0);
    }

    /**
     * 跳转到目录页
     */
    public static PdfAction getCatalogPageAction(PdfDocument pdf, int pageNum) {
        PdfDestination dest = PdfExplicitDestination.createXYZ(pdf.getPage(pageNum), 60, -10, 1);
        return PdfAction.createGoTo(dest);
    }
}
